package com.xx.xiawan.net;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;


/**
 * Created by Administrator on 2017-08-11.
 */

public class GetRequest extends DGRequestBase<String> {
    protected Context context;
    private RequestQueue mRequestQueue;
    private String mResponse;
    protected DGStringRequest stringRequest;


    public GetRequest(String url, Context context, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(context, null);
        this.context = context;
        stringRequest = new DGStringRequest(url, listener, errorListener);
        mRequest = stringRequest;
        //解决有问题的代码
    }

    public GetRequest(int method, String url, Context context, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(context, null);
        this.context = context;
        stringRequest = new DGStringRequest(method, url, listener, errorListener);
        mRequest = stringRequest;
        //解决有问题的代码
    }

    public void header(String name, String value) {
        stringRequest.header(name, value);
    }

    public void setParamsMap(Map<String, String> paramsMap) {
        stringRequest.setParamsMap(paramsMap);
    }

    public GetRequest(String url, Context context) {
        super(context, null);
        this.context = context;
        init(url);
    }

    public GetRequest(String url, Context context, DGRequestListener listener) {
        super(context, listener);
        this.context = context;
        init(url);
    }

    protected void init(String url) {
        stringRequest = new DGStringRequest(url, this, this);
        mRequest = stringRequest;
    }

    public void onResponse(String response) {
        Log.e("drj", " get response is " + response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            int code = jsonObject.getInt(DGNetUtil.CODE_KEY);
            if (code == DGNetUtil.REQUEST_SUCCESS) {
//                Log.e("res", " get response is " + response);
                jsonResult.result = jsonObject.getString(DGNetUtil.CONTENT_KEY);
//                Log.e("res", "  jsonResult.result " + jsonResult.result);
//                Log.e("res", "  mRequestListener " + mRequestListener);
                if (mRequestListener != null) {
                    mRequestListener.onSuccess(jsonResult);
                }
            } else {
                jsonResult.errorCode = DGJsonResult.REQUEST_ERROR;
                String content = jsonObject.getString(DGNetUtil.CONTENT_KEY);
                JSONObject erroJson = new JSONObject(content);
                int errorCode = erroJson.getInt(DGNetUtil.CODE_KEY);
                if (errorCode == DGNetUtil.REQUEST_NEED_LOGIN) {
                    jsonResult.errorCode = errorCode;
                    jsonResult.result = content;
                    if (mRequestListener != null) {
                        mRequestListener.onSuccess(jsonResult);
                    }
                } else {
                    String error = erroJson.getString(DGNetUtil.ERROR_MSG_KEY);
                    if (mRequestListener != null) {
                        mRequestListener.onError(error);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        unLock();
    }
}
